# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)
set(PICO_SDK_PATH "C:/PICO_SDK/PicoSDK151/pico-sdk")
set(picotool_HINT ${PICO_SDK_PATH}/picotool/2.0.0/picotool)
set(PICO_BOARD pico CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")       # 设置 C 文件优化
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")   # 设置 C++ 文件优化
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
  message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

project(CotexM0_pico_board C CXX ASM)

set(PICO_CXX_ENABLE_EXCEPTIONS 1)

set(PICO_CXX_ENABLE_RTTI 1)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

#config kernel path
set(TobudOS_ROOT_DIR ../..)
set(TobudOS_KNL_DIR ${TobudOS_ROOT_DIR}/kernel)
#config port path
#单核版本
# set(TobudOS_PORT_SRC
#     ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/cortex-m0+/gcc/port_c.c
#     ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/cortex-m0+/gcc/port_s.S
#     ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/tos_cpu.c    
#     ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/tos_fault.c    
# )
# smp 版本
set(TobudOS_PORT_SRC
    ${TobudOS_KNL_DIR}/arch/arm/pico-RP2040/gcc/port_c.c
    ${TobudOS_KNL_DIR}/arch/arm/pico-RP2040/gcc/port_s.S
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/tos_cpu.c    
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/tos_fault.c    
)
file(GLOB TobudOS_SYS_SRC 
    ${TobudOS_KNL_DIR}/core/*.c
    ${TobudOS_KNL_DIR}/osal/cmsis_os/cmsis_os.c
    ${TobudOS_ROOT_DIR}/test/*.c # For tests
)

set(TobudOS_INC 
    ${TobudOS_KNL_DIR}/core/include
    #${TobudOS_KNL_DIR}/arch/arm/arm-v7m/cortex-m0+/gcc #单核
    ${TobudOS_KNL_DIR}/arch/arm/pico-RP2040/gcc #SMP
    ${TobudOS_KNL_DIR}/arch/arm/arm-v7m/common/include
    ${TobudOS_KNL_DIR}/osal/cmsis_os
    ${TobudOS_ROOT_DIR}/test/include # For tests
)

# Add executable. Default name is the project name, version 0.1

add_executable(CotexM0_pico_board 
${TobudOS_PORT_SRC}
${TobudOS_SYS_SRC} 
CotexM0_pico_board.cpp )

target_compile_options(CotexM0_pico_board PRIVATE
    -Wno-write-strings
)


# target_link_options(CotexM0_pico_board PRIVATE
#     -Wl,--defsym,isr_pendsv=PendSV_Handler
#     -Wl,--defsym,isr_systick=SysTick_Handler
# )
#SMP
target_link_options(CotexM0_pico_board PRIVATE
    -Wl,--defsym,isr_pendsv=PendSV_smp_Handler
    -Wl,--defsym,isr_systick=SysTick_Handler
)

pico_set_program_name(CotexM0_pico_board "CotexM0_pico_board")
pico_set_program_version(CotexM0_pico_board "0.1")

pico_enable_stdio_uart(CotexM0_pico_board 0)
pico_enable_stdio_usb(CotexM0_pico_board 1)

# Add the standard library to the build
target_link_libraries(  CotexM0_pico_board
        pico_stdlib)
target_link_libraries(CotexM0_pico_board 
         hardware_exception
         hardware_timer
         hardware_watchdog
         hardware_clocks
         pico_base_headers    
         pico_multicore
         )
 
# Add the standard include files to the build
target_include_directories(CotexM0_pico_board PRIVATE
  ${CMAKE_CURRENT_LIST_DIR}
  ${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
  ${PICO_SDK_PATH}/src/rp2_common/cmsis/stub/CMSIS/Device/RaspberryPi/RP2040/Include
  ${PICO_SDK_PATH}/src/rp2_common/cmsis/include/cmsis
  ${PICO_SDK_PATH}/src/rp2_common/hardware_regs/include
  ${PICO_SDK_PATH}/src/rp2_common/hardware_base/include
  ${PICO_SDK_PATH}/src/rp2_common/cmsis/stub/CMSIS/Core/Include
  ${TobudOS_INC}
  ${CMAKE_CURRENT_LIST_DIR}/TOS_CONFIG
  ${PICO_SDK_PATH}/src/common/pico_stdlib/include

  )

# Add any user requested libraries

pico_add_extra_outputs(CotexM0_pico_board)
